/*******************************************************************************
 * Name   : main_test.cc
 * Date   : 01-17-2015
 * Author : Sylvain Nieuwlandt -- nicholas.rush.developper@gmail.com
 ******************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "AbstractTestClass.hpp"

/* PROJECT CLASS TO TEST */
#include "KiObjectTestClass.hpp"
#include "KiBooleanTestClass.hpp"

static char *errorsMessages = NULL;
static int numberOfClassTested = 0;
static int numberOfSuccess = 0;

void runTestClass(AbstractTestClass *testClass) {
  char *errors;
  int size;

  printf("Running tests of %s class...", testClass->getClassName());
  testClass->run();
 
  if (testClass->isFailed()) {
    printf("KO\n");
    errors = testClass->getErrors();
    size = strlen(errorsMessages) + strlen(errors);
    snprintf(errorsMessages, size + 1, "%s%s", errorsMessages, errors);
    errorsMessages[size] = '\0';
  } else {
    printf("OK\n");
    numberOfSuccess++;
  }

  numberOfClassTested++;
}

int main() {
  errorsMessages = (char*) malloc(sizeof(char));
  printf("==================================================\n");
  printf("== Start the Unit test run\n");
  printf("==================================================\n");



/* -- ADD TEST CLASS HERE !! -------------------------------------------------*/
  runTestClass(new KiObjectTestClass());
  runTestClass(new KiBooleanTestClass());
/* -- END OF TEST CLASS ------------------------------------------------------*/


  printf("%s\n\n", errorsMessages);

  printf("RUN %s [%d class in success on %d class tested]\n",
	 (numberOfSuccess == numberOfClassTested) ? "SUCCESS" : "FAILURE",
	 numberOfSuccess, numberOfClassTested);

  /* Don't need to free this, end of program and it's not an opened file. */
  return numberOfClassTested - numberOfSuccess;
}
